//
//  ReseterController.m
//  who
//
//  Created by yunqi-dev-1 on 15/8/26.
//  Copyright (c) 2015年 yunqi-dev-1. All rights reserved.
//

#import "ReseterController.h"
#import "DXKit.h"
#import "AppDelegate.h"

@implementation ReseterController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
}

- (IBAction)clickVFC:(UIButton *)sender {
    NSString *userPhone = _phoneTextField.text;
    if (userPhone.length == 0) {
        [DX alert:@"请输入手机号"];
        return;
    }
    NSDictionary *params = @{@"phone": userPhone};
    [DX POST:API_ACTION_PHONE_VERIFICATION_CODE_SEND parameters:params controller:self success:^(id responseObject) {
        NSDictionary *response = (NSDictionary *)responseObject;
        NSNumber *code = response[@"code"];
        if (code.integerValue == 0) {
            [DX alert:@"验证码已发送"];
            _verificationButton.enabled = false;
            [_verificationButton setBackgroundColor:[UIColor grayColor]];
            num = 0;
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(verificationChange) userInfo:nil repeats:YES];
        }
        else
        {
            if (code.integerValue == ERROR_EXIST)
            {
                [DX alert:@"用户已存在"];
            }
            else
            {
                [DX alert:@"请输入正确的手机号"];
            }
        }
    } failure:nil failureAlert:YES];
}

- (IBAction)clickReseter:(UIButton *)sender {
    if ([self.phoneTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入手机号" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    if ([self.passWordTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入密码" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    if ([self.isPassWorldTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入确认密码" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    if ([self.vfcTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入验证码" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    if (![self.passWordTextField.text isEqualToString:self.isPassWorldTextField.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"两次密码输入不一致" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    NSString *verificationCode = [self.vfcTextField text];
    NSDictionary *params = @{@"phone": self.phoneTextField.text,@"password": self.passWordTextField,@"code": verificationCode};
    [DX POST:API_ACTION_USER_REGISTER parameters:params controller:self success:^(NSDictionary *response) {
        NSLog(@"%@",response);
        NSString *code = response[@"code"];
        if (code.integerValue == 9) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"用户已存在" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
        }
        else if (code.integerValue == 0)
        {
            NSString *token = response[@"token"];
            NSDictionary *user = response[@"user"];
            DXData *data = [DXData sharedInstance];
            data.token = token;
            data.user = [user mutableCopy];
            [data save];

            [[AppDelegate getAppDelegate] refreshRootViewControllerWithStoryboardName:@"main" animation:YES];
        }
    } failure:nil failureAlert:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.phoneTextField resignFirstResponder];
    [self.passWordTextField resignFirstResponder];
    [self.isPassWorldTextField resignFirstResponder];
    [self.vfcTextField resignFirstResponder];
}

- (void) verificationChange
{
    if (num < 60)
    {
        _verificationButton.titleLabel.text = [NSString stringWithFormat:@"发送验证码(%d秒)", 60 -num];
        [_verificationButton setTitle:[NSString stringWithFormat:@"发送验证码(%d秒)", 60 -num] forState:UIControlStateDisabled];
        num++;
    }
    else{
        _verificationButton.enabled = true;
        [_verificationButton setBackgroundColor:[DX colorWithHex:@"3a5591"]];
        [timer invalidate];
    }
}
@end
